package org.xq.softcup.mq.admin.storage;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;

/**
 * mq消息内容管理
 */
@Slf4j
@Data
@Component
public class MqStorageManager {
    //目前当前路径
    private String rootPath;
    //消息存放目录
    @Value("${mq.store.path}")
    private String mqBasePath;
    //消息存储对象
    private MqStorage mqStorage;

    /**
     * 初始化文件目录
     */
    @PostConstruct
    public  void initConfig() throws IOException {
        //检查mq存放目录是否存在, 不存在则创建目录
        checkBasePath();
        getMqStorage();
    }

    /**
     * 获数据取存储对象
     * @return
     */
    public synchronized MqStorage getMqStorage() {
        if (mqStorage == null) {
            mqStorage = new MqStorage();
            mqStorage.setBasePath(mqBasePath); //设置基础变量
            mqStorage.getMqFileHolder();
        }
        return mqStorage;
    }
    /**
     * 检查和创建目录
     * @return
     */
    private String checkBasePath() {
        try {
            //当前项目下路径
            rootPath = System.getProperty("user.dir");
            File folder = new File(rootPath+mqBasePath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            mqBasePath = folder.getPath();
            return mqBasePath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取当前topic的mq文件全路径
   /* public String getMqFullPath(String topic, String queueName) {
        MqStorage mqStorage = null;
        if (StringUtils.isEmpty(queueName)) {
            mqStorage = mqStoreMap.get(topic);
        } else {
            mqStorage = mqStoreMap.get(topic + "_" + queueName);
        }
        return mqStorage.getMqPath();
    }*/


}
